package ar.com.jendrix.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateUtil {
	// Date formats.
	public static final String SIMPLE_DATE_FORMAT = "dd/MM/yyyy";
	public static final String FULL_DATE_FORMAT = "dd/MM/yyyy hh:mm:ss a";

	// Prebuilt dates.
	//public static final Date EMPTY_DATE = getDate("01/01/1900");
	//public static final Date FINAL_DATE = getDate("01/01/5000");

	/**
     */
	public static final Date getDate(String stringDate, String format) {
		Date date;
		try {
			date = new SimpleDateFormat(format).parse(stringDate);
		} catch (ParseException p) {
			date = null;
		}
		return date;
	}

	/**
     */
	public static final Date getDate(String stringDate) {
		return getDate(stringDate, SIMPLE_DATE_FORMAT);
	}

	/**
     */
	public static final String getDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
     */
	public static final String getDate(Date date) {
		return getDate(date, SIMPLE_DATE_FORMAT);
	}

	/**
     */
	public static final Date getFirstInstantOfDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.set(Calendar.AM_PM, Calendar.AM);
		return c.getTime();
	}

	/**
     */
	public static final Date getLastInstantOfDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR, 11);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		c.set(Calendar.AM_PM, Calendar.PM);
		return c.getTime();
	}

	/**
     */
	public static final Date addDays(Date date, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, days);
		return c.getTime();
	}

	/**
     */
	public static final Date addMonths(Date date, int months) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, months);
		return c.getTime();
	}

	/**
     */
	public static final Date addYears(Date date, int years) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, years);
		return c.getTime();
	}

	/**
     */
	public static final long getTime() {
		return System.currentTimeMillis();
	}

	/**
     */
	public static final Integer getDayOfWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_WEEK);
	}

	/**
     */
	public static final Date getWorkingDate(Date date) {
		Integer dow = getDayOfWeek(date);
		int addDays = 0;

		if (dow.equals(Calendar.SATURDAY)) {
			addDays = 2;
		} else if (dow.equals(Calendar.SUNDAY)) {
			addDays = 1;
		}
		return addDays(date, addDays);
	}
	
	public static final String format(Date fecha) {
		String fechaString = "";
		if (fecha != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			fechaString = sdf.format(fecha);
		}
		return fechaString;
	}

	public static final String format(Date date, String pattern) {
		String dateFormat = "";
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			dateFormat = sdf.format(date);
		}
		return dateFormat;
	}
	
}